package com.sanpu.kxe.business.trade.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sanpu.kxe.R;
import com.sanpu.kxe.business.trade.bean.TransactionOutput;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.utils.DateUtils;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.ArrayList;
import java.util.Date;

/**
 * Description：账单列表
 * Created by 薛勇军 on 2018/8/27.
 */

public class TransactionAdapter extends BaseQuickAdapter<TransactionOutput, BaseViewHolder>
        implements StickyRecyclerHeadersAdapter<BaseViewHolder> {

    private LayoutInflater inflater;

    public TransactionAdapter(Context context) {
        super(R.layout.item_transaction, new ArrayList<TransactionOutput>());
        inflater = LayoutInflater.from(context);
    }

    @Override
    protected void convert(BaseViewHolder helper, TransactionOutput item) {
        try {
            if (item != null) {
                helper.setText(R.id.pay_state, !TextUtils.isEmpty(item.getPayStatus()) ? item.getPayStatus() : "")
                        .setText(R.id.money, item.getAmount() + "元")
                        .setText(R.id.pay_type,
                                !TextUtils.isEmpty(item.getTransactionName()) ? item.getTransactionName() : "");

                String time = DateUtils.formatTime(item.getCreateTime());
                helper.setText(R.id.date, time);

                TextView moneyTv = helper.getView(R.id.money);
                if (!TextUtils.isEmpty(item.getAmount())) {
                    moneyTv.setText(item.getAmount());
                }
                ImageView iconIv = helper.getView(R.id.icon);
                iconIv.setImageResource(CommonUtils.getPayDrawable(item.getTradeSource()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public long getHeaderId(int position) {
        TransactionOutput output = getItem(position);
        if (output == null) return -1;
        String time = output.getCreateTime();
        if (TextUtils.isEmpty(time) || time.length() < 7) return 0;
        Date date = DateUtils.string2Date(time.substring(0, 7), "yyyy-MM");
        return date.getTime();
    }

    @Override
    public BaseViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return new BaseViewHolder(inflater.inflate(R.layout.item_transaction_head, parent, false));
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int position) {
        TransactionOutput output = getItem(position);
        if (output != null && output.getCreateTime() != null && output.getCreateTime().length() > 6) {
            holder.setText(R.id.month, output.getCreateTime().substring(0, 7));
        }
    }
}
